package com.atung.parking.controller;

import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.config.security.RequestWrapper;
import com.atung.parking.config.tlPay.PayService;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.OrderState;
import com.atung.parking.enums.OrderType;
import com.atung.parking.request.query.QueryOrdersRequest;
import com.atung.parking.request.save.SaveOrUpdateOrderRequest;
import com.atung.parking.request.save.SaveOrUpdatePackageOrderRequest;
import com.atung.parking.response.page.BillOrderResponse;
import com.atung.parking.response.page.OneselfOrderParkingResponse;
import com.atung.parking.response.page.ParkingOrdersResponse;
import com.atung.parking.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Administrator
 */
@RestController
@Api(tags = "订单管理中心")
@RequestMapping("/api/order")
public class OrderController {
    @Resource
    private PayService payService;
    @Resource
    private OrderService orderService;

    @GetMapping("/query/parking")
    @ApiOperation("获取预定订单列表")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult<PageResponse<ParkingOrdersResponse>> queryOrderParkingList(QueryOrdersRequest request) {
        PageInfoResponse<ParkingOrdersResponse> pageInfoResponse = orderService.queryOrderParkingListService(request);
        return ApiResult.ok(pageInfoResponse.getData(), request.getPageNum(), request.getPageSize(), pageInfoResponse.getTotal());
    }

    @GetMapping("/parking/export")
    @ApiOperation("导出预订订单数据")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public void exportOrderParking(QueryOrdersRequest request, HttpServletResponse httpServletResponse) {
        orderService.exportOrderParkingService(request, httpServletResponse);
    }

    @GetMapping("/query/package")
    @ApiOperation("获取套餐订单列表")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult<PageResponse<ParkingOrdersResponse>> queryOrderParkingPackageList(QueryOrdersRequest request) {
        PageInfoResponse<ParkingOrdersResponse> pageInfoResponse = orderService.queryOrderParkingPackageList(request);
        return ApiResult.ok(pageInfoResponse.getData(), request.getPageNum(), request.getPageSize(), pageInfoResponse.getTotal());
    }

    @GetMapping("/package/export")
    @ApiOperation("导出套餐订订单数据")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public void exportOrderParkingPackage(QueryOrdersRequest request, HttpServletResponse httpServletResponse) {
        orderService.exportOrderParkingPackageService(request, httpServletResponse);
    }

    @GetMapping("/query/revenue")
    @ApiOperation("获取当日订单总营收")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult<BigDecimal> queryRevenue(OrderType orderType) {
        if (Objects.isNull(orderType)) {
            return ApiResult.fail("请选择订单类型");
        }
        BigDecimal revenue = orderService.queryRevenueService(orderType);
        return ApiResult.ok(revenue);
    }

    @PostMapping("/save/parking")
    @ApiOperation("新增车位订单（小程序）")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult<BillOrderResponse> saveParkingOrder(@RequestBody @Valid SaveOrUpdateOrderRequest request) {
        try {
            BillOrderResponse billOrderResponse = orderService.saveParkingOrderService(request);
            return ApiResult.ok(billOrderResponse);
        } catch (Exception e) {
            return ApiResult.fail(e.getMessage());
        }
    }

    @PostMapping("/save/package")
    @ApiOperation("新增套餐订单（小程序）")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult<BillOrderResponse> saveParkingPackageOrder(@RequestBody @Valid SaveOrUpdatePackageOrderRequest request) {
        try {
            BillOrderResponse billOrderResponse = orderService.saveParkingPackageOrderService(request);
            return ApiResult.ok(billOrderResponse);
        } catch (Exception e) {
            return ApiResult.fail(e.getMessage());
        }

    }

    @GetMapping("/query/oneself")
    @ApiOperation("获取我的订单列表(小程序)")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult<List<OneselfOrderParkingResponse>> oneselfOrderParkingList(OrderState orderState) {
        List<OneselfOrderParkingResponse> responses = orderService.oneselfOrderParkingListService(orderState);
        return ApiResult.ok(responses);
    }

    @GetMapping("/query/details/{id}")
    @ApiOperation("用户获取订单详情（小程序）")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult oneselfOrderDetails(@PathVariable("id") Long id) {

        return orderService.oneselfOrderDetailsService(id);
    }

    @RequestMapping("/callback")
    @ApiOperation("监听支付回调地址")
    public void handleCallback(RequestWrapper request, HttpServletResponse response) throws ServletException, IOException {
        try {
            payService.handleCallbackService(request,response);
        }catch (Exception exception){

        }
    }

    @PostMapping("/cancel/{id}")
    @ApiOperation("用户取消订单（小程序）")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult oneselfOrderCancel(@PathVariable("id") Long id) {
        return ApiResult.ok();
    }
}
